//
//  ByExcelBaseView.m
//  laserdevice_ios
//
//  Created by pro on 16/12/12.
//  Copyright © 2016年 com.itboye. All rights reserved.
//

#import "ByExcelBaseView.h"

#import "ByExcelLayerView.h"


// 数据测量model
#import "ByBlueToothMeaturesL1Model.h"          // 单次测量
#import "ByBlueToothMeaturesL2Model.h"          // 单次测量
#import "ByBlueToothMeaturesL3Model.h"          // 单次测量
#import "ByBlueToothMeaturesG1Model.h"          // 单次勾股
#import "ByBlueToothMeaturesG2Model.h"          // 双次勾股加
#import "ByBlueToothMeaturesG3Model.h"          // 双次勾股减
#import "ByBlueToothMeaturesS1Model.h"          // 长方形面积
#import "ByBlueToothMeaturesS2Model.h"          // 三角形面积
#import "ByBlueToothMeaturesS3Model.h"          // 梯形面积
#import "ByBlueToothMeaturesV1Model.h"          // 长方体体积
#import "ByBlueToothMeaturesH1Model.h"          // 三角测量
#import "ByBlueToothMeaturesH2Model.h"          // 直角测量
#import "ByBlueToothMeaturesX1Model.h"          // 距离+水平
#import "ByBlueToothMeaturesP1Model.h"          // 加法
#import "ByBlueToothMeaturesM1Model.h"          // 减法


@implementation ByExcelBaseView
{
    // view
    ByExcelLayerView *_formLineView;
    
    // data
    NSInteger _lines;
}
#define kNumofColumn 5
#define KScale ([ByCommonCache getSystemLanguage].intValue == 1 ? 2 : 1)
#define kSingleHeight 25
#define kSingleWidth (100 * KScale)

// 模拟数据
- (NSDictionary *)simData {
    
    ByBlueToothMeaturesL1Model *l1Model = [[ByBlueToothMeaturesL1Model alloc] init];
    l1Model.name = @"L1模型";
    l1Model.length = 2.3;
    ByBlueToothMeaturesG1Model *g1Model = [[ByBlueToothMeaturesG1Model alloc] init];
    g1Model.name = @"G1模型";
    g1Model.length1 = 2.3;
    g1Model.length2 = 2.3;
    g1Model.length3 = 2.3;
    ByBlueToothMeaturesG2Model *g2Model = [[ByBlueToothMeaturesG2Model alloc] init];
    g2Model.name = @"G2模型";
    g2Model.length1 = 2.3;
    g2Model.length2 = 2.3;
    g2Model.length3 = 2.3;
    g2Model.length4 = 2.3;
    ByBlueToothMeaturesG3Model *g3Model = [[ByBlueToothMeaturesG3Model alloc] init];
    g3Model.name = @"G3模型";
    g3Model.length1 = 2.3;
    g3Model.length2 = 2.3;
    g3Model.length3 = 2.3;
    g3Model.length4 = 2.3;
    ByBlueToothMeaturesS1Model *s1Model = [[ByBlueToothMeaturesS1Model alloc] init];
    s1Model.name = @"S1模型";
    s1Model.length1 = 2.3;
    s1Model.length2 = 2.3;
    s1Model.area = 5;
    ByBlueToothMeaturesS2Model *s2Model = [[ByBlueToothMeaturesS2Model alloc] init];
    s2Model.name = @"S2模型";
    s2Model.length1 = 2.3;
    s2Model.length2 = 2.3;
    s2Model.length3 = 2.3;
    s2Model.area = 2.3;
    ByBlueToothMeaturesS3Model *s3Model = [[ByBlueToothMeaturesS3Model alloc] init];
    s3Model.name = @"S3模型";
    s3Model.topLength = 2.3;
    s3Model.bottomLength = 2.3;
    s3Model.height = 2.3;
    s3Model.area = 2.3;
    ByBlueToothMeaturesV1Model *v1Model = [[ByBlueToothMeaturesV1Model alloc] init];
    v1Model.name = @"V1模型";
    v1Model.length = 2.3;
    v1Model.width = 2.3;
    v1Model.height = 2.3;
    v1Model.volume = 2.3;
    ByBlueToothMeaturesH2Model *h2Model = [[ByBlueToothMeaturesH2Model alloc] init];
    h2Model.name = @"H2模型";
    h2Model.angle = 55.0;
    h2Model.hypotenuseLength = 2.3;
    h2Model.horizontalLength = 2.3;
    h2Model.verticalLength = 2.3;
    return @{@"L1" : @[l1Model],
             @"G1" : @[g1Model],
             @"G2" : @[g2Model],
             @"G3" : @[g3Model],
             @"S1" : @[s1Model],
             @"S2" : @[s2Model],
             @"S3" : @[s3Model],
             @"V1" : @[v1Model],
             @"H2" : @[h2Model]};
}

- (NSMutableDictionary *)saveDic {
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    
    NSString *create_time = [NSDate getCurrentDateForStringFormatWithFormatter:nil];
    [dic setObject:create_time forKey:@"create_time"];
    
    NSMutableArray *type_list = [[NSMutableArray alloc] init];
    for (NSString *type in _meatureModelsDic) {
        NSMutableDictionary *typeDic = [[NSMutableDictionary alloc] init];
        //
        [typeDic setObject:type forKey:@"type"];
        NSMutableArray *element_list = [[NSMutableArray alloc] init];
        NSArray *models = _meatureModelsDic[type];
        for (ByBlueToothMeaturesModel *model in models) {
            [element_list addObject:[model saveDic]];
        }
        //
        [typeDic setObject:element_list.copy forKey:@"element_list"];
        
        [type_list addObject:typeDic.copy];
    }
    
    [dic setObject:type_list forKey:@"type_list"];
    
    return dic;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        _meatureModelsDic = [self simData];
        
        _lines = 1;
        for (NSString *key in _meatureModelsDic.allKeys) {
            NSArray *models = _meatureModelsDic[key];
            _lines ++;
            _lines += models.count;
            _lines ++;
        }
        self.contentSize = CGSizeMake(kNumofColumn * kSingleWidth + kSingleHeight, _lines * kSingleHeight);
        self.bounces = NO;
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        
        [self setupUI];
        [self drawForm];
    }
    return self;
}

- (void)setMeatureModelsDic:(NSDictionary *)meatureModelsDic {
    
    _meatureModelsDic = meatureModelsDic;
    
    // 先移除
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    _lines = 1;
    for (NSString *key in _meatureModelsDic.allKeys) {
        NSArray *models = _meatureModelsDic[key];
        _lines ++;  // 测量类型和对应属性，占一行
        _lines += models.count;
        _lines ++;  // 空一格
    }
    _lines --;      // 最后个测量类型不用空格
    
    self.contentSize = CGSizeMake(kNumofColumn * kSingleWidth + kSingleHeight, _lines * kSingleHeight);
    self.bounces = NO;
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    
    [self setupUI];
    [self drawForm];
    
    // 关闭loading动画
    [ByCoreToolAlert hideHud];
}

// 绘制表格
- (void)drawForm {
    
    _formLineView = [[ByExcelLayerView alloc] init];
    _formLineView.shapeLayer.lineWidth = 0.5;
    _formLineView.shapeLayer.fillColor = nil;
    _formLineView.shapeLayer.strokeColor = [UIColor colorWithWhite:0.7 alpha:1].CGColor;
    _formLineView.backgroundColor = [UIColor clearColor];
    _formLineView.opaque = NO;
    [self addSubview:_formLineView];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    // 横线
    for (int i = 0; i <= _lines; i++) {
        [path moveToPoint:CGPointMake(0, i * kSingleHeight)];
        [path addLineToPoint:CGPointMake(self.contentSize.width, i * kSingleHeight)];
    }
    // 竖线
    for (int i = 0; i <= kNumofColumn + 1; i++) {
        if (i == 1) {
            [path moveToPoint:CGPointMake(i * kSingleHeight, 0)];
            [path addLineToPoint:CGPointMake(i * kSingleHeight, self.contentSize.height)];
        } else {
            [path moveToPoint:CGPointMake((i - 1) * kSingleWidth + kSingleHeight, 0)];
            [path addLineToPoint:CGPointMake((i - 1) * kSingleWidth + kSingleHeight, self.contentSize.height)];
        }
    }
    
    _formLineView.shapeLayer.path = path.CGPath;
}

- (void)setupUI {
    
    CGFloat x, y, width, height;
    // 左上角的图标
    x = 0;
    y = 0;
    width = kSingleHeight;
    height = kSingleHeight;
    UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    iconView.image = [UIImage imageNamed:@"dataImport_icon"];
    [self addSubview:iconView];
    
    // 第一行的标题（A,B,C...）
    NSArray *topTitles = @[@"A", @"B", @"C", @"D", @"E"];
    for (int i = 0; i < topTitles.count; i++) {
        x = iconView.right + i * kSingleWidth;
        y = 0;
        width = kSingleWidth;
        height = kSingleHeight;
        UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        topLabel.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
        topLabel.adjustsFontSizeToFitWidth = YES;
        topLabel.font = FONT(14);
        topLabel.text = topTitles[i];
        topLabel.textColor = [UIColor blackColor];
        topLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:topLabel];
    }
    
    // 第一列的标题(1,2,3,4...)
    for (int i = 0; i < _lines - 1; i++) {
        x = 0;
        y = iconView.bottom + i * kSingleHeight;
        width = kSingleHeight;
        height = kSingleHeight;
        UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        leftLabel.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
        leftLabel.adjustsFontSizeToFitWidth = YES;
        leftLabel.font = FONT(14);
        leftLabel.text = [NSString stringWithFormat:@"%i", i + 1];
        leftLabel.textColor = [UIColor blackColor];
        leftLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:leftLabel];
    }
    
    // 把键名数组进行排序
    NSArray *sortKeys = self.meatureModelsDic.allKeys;
    sortKeys = [sortKeys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
        NSComparisonResult result = [obj1 compare:obj2];
        return result == NSOrderedDescending;
    }];
    
    NSInteger curLine = 1;
    // 根据字典的key来判断类型，创建动态标题
    for (NSString *key in sortKeys) {
        
        NSArray *models = self.meatureModelsDic[key];

        if ([key isEqualToString:@"L1"]) {
//            [self createLabelsWithTextArray:@[@"单次测量", @"长度"]
//                                    curLine:curLine
//                                    canEdit:NO
//                                        tag:0];
            [self createLabelsWithTextArray:@[STRING(@"danciceliang"), STRING(@"changdu")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesL1Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:100 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;
        }
        if ([key isEqualToString:@"L2"]) {
//            [self createLabelsWithTextArray:@[@"连续测量", @"最小长度", @"最大长度", @"最后测量"]
//                                    curLine:curLine
//                                    canEdit:NO
//                                        tag:0];
            [self createLabelsWithTextArray:@[STRING(@"lianxuceliang"), STRING(@"zuixiaochangdu"), STRING(@"zuidachangdu"), STRING(@"zuihouceliang")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesL2Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:200 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;
        }
        if ([key isEqualToString:@"L3"]) {
//            [self createLabelsWithTextArray:@[@"实时测量", @"长度"]
//                                    curLine:curLine
//                                    canEdit:NO
//                                        tag:0];
            [self createLabelsWithTextArray:@[STRING(@"shishiceliang"), STRING(@"changdu")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesL3Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:300 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;
        }
        
        if ([key isEqualToString:@"G1"]) {
//            [self createLabelsWithTextArray:@[@"单次勾股", @"斜边", @"水平边", @"垂直边"]
//                                    curLine:curLine
//                                    canEdit:NO
//                                        tag:0];
            [self createLabelsWithTextArray:@[STRING(@"dancigougu"), STRING(@"xiebian"), STRING(@"shuipingbian"), STRING(@"chuizhibian")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesG1Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:400 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;

        }
        
        if ([key isEqualToString:@"G2"]) {
//            [self createLabelsWithTextArray:@[@"双次勾股加", @"斜边1", @"水平边", @"斜边2", @"垂直边"]
//                                    curLine:curLine
//                                    canEdit:NO
//                                        tag:0];
            [self createLabelsWithTextArray:@[STRING(@"shuangcigougujia"), STRING(@"xiebian1"), STRING(@"shuipingbian"), STRING(@"xiebian2"), STRING(@"chuizhibian")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesG2Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:500 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;

        }
        
        if ([key isEqualToString:@"G3"]) {
//            [self createLabelsWithTextArray:@[@"双次勾股减", @"斜边1", @"斜边2", @"水平边", @"垂直边"]
//                                    curLine:curLine
//                                    canEdit:NO
//                                        tag:0];
            [self createLabelsWithTextArray:@[STRING(@"shuangcigougujian"), STRING(@"xiebian1"), STRING(@"xiebian2"), STRING(@"shuipingbian"), STRING(@"chuizhibian")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesG3Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:600 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;

        }
        
        if ([key isEqualToString:@"S1"]) {
//            [self createLabelsWithTextArray:@[@"长方形面积", @"长", @"宽", @"面积"]
//                                    curLine:curLine
//                                    canEdit:NO
//                                        tag:0];
            [self createLabelsWithTextArray:@[STRING(@"changfangxingmianji"), STRING(@"chang"), STRING(@"kuan"), STRING(@"mianji")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesS1Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:700 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;
            
        }
        
        if ([key isEqualToString:@"S2"]) {
//            [self createLabelsWithTextArray:@[@"三角形面积", @"边长1", @"边长2", @"边长3", @"面积"]
//                                    curLine:curLine
//                                    canEdit:NO
//                                        tag:0];
            [self createLabelsWithTextArray:@[STRING(@"sanjiaoxingmianji"), STRING(@"bianchang1"), STRING(@"bianchang2"), STRING(@"bianchang3"), STRING(@"mianji")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesS2Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:800 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;

        }
        
        if ([key isEqualToString:@"S3"]) {
//            [self createLabelsWithTextArray:@[@"梯形面积", @"上底", @"下底", @"高", @"面积"]
//                                    curLine:curLine
//                                    canEdit:NO
//                                        tag:0];
            [self createLabelsWithTextArray:@[STRING(@"tixingmianji"), STRING(@"shangdi"), STRING(@"xiadi"), STRING(@"gao"), STRING(@"mianji")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesS3Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:900 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;

        }
        
        
        if ([key isEqualToString:@"V1"]) {
//            [self createLabelsWithTextArray:@[@"长方体体积", @"长", @"宽", @"高", @"体积"]
//                                    curLine:curLine
//                                    canEdit:NO
//                                        tag:0];
            [self createLabelsWithTextArray:@[STRING(@"changfangtitiji"), STRING(@"chang"), STRING(@"kuan"), STRING(@"gao"), STRING(@"tiji")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesV1Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:1000 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;

        }
        
        if ([key isEqualToString:@"H1"]) {
//            [self createLabelsWithTextArray:@[@"三角测量", @"顶角", @"边长1", @"边长2", @"边长3"]
//                                    curLine:curLine
//                                    canEdit:NO
//                                        tag:0];
            [self createLabelsWithTextArray:@[STRING(@"sanjiaoceliang"), STRING(@"dingjiao"), STRING(@"bianchang1"), STRING(@"bianchang2"), STRING(@"bianchang3")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesH1Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:1100 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;
            
        }
        if ([key isEqualToString:@"H2"]) {
//            [self createLabelsWithTextArray:@[@"直角测量", @"水平倾角", @"斜边", @"水平边", @"垂直边"]
//                                    curLine:curLine
//                                    canEdit:NO
//                                        tag:0];
            [self createLabelsWithTextArray:@[STRING(@"zhijiaoceliang"), STRING(@"shuipingqingjiao"), STRING(@"xiebian"), STRING(@"shuipingbian"), STRING(@"chuizhibian")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesH2Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:1200 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;
        }
        if ([key isEqualToString:@"X1"]) {
//            [self createLabelsWithTextArray:@[@"距离+水平", @"角度X", @"角度Y", @"长度"]
//                                    curLine:curLine
//                                    canEdit:NO
//                                        tag:0];
            [self createLabelsWithTextArray:@[STRING(@"juli+shuiping"), STRING(@"jiaoduX"), STRING(@"jiaoduY"), STRING(@"changdu")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesX1Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:1300 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;
        }
        if ([key isEqualToString:@"P1"]) {
//            [self createLabelsWithTextArray:@[@"加法测量", @"长度1", @"长度1", @"长度3"]
//                                    curLine:curLine
//                                    canEdit:NO
//                                        tag:0];
            [self createLabelsWithTextArray:@[STRING(@"jiafaceliang"), STRING(@"changdu1"), STRING(@"changdu2"), STRING(@"changdu3")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesP1Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:1400 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;
        }
        if ([key isEqualToString:@"M1"]) {
            [self createLabelsWithTextArray:@[STRING(@"jianfaceliang"), STRING(@"changdu1"), STRING(@"changdu2"), STRING(@"changdu3")]
                                    curLine:curLine
                                    canEdit:NO
                                        tag:0];
            curLine ++;
            for (ByBlueToothMeaturesM1Model *model in models) {
                [self createLabelsWithTextArray:[self textArrayFromModel:model]
                                        curLine:curLine
                                        canEdit:YES
                                            tag:1500 + ([models indexOfObject:model] + 1) * 10];
                curLine ++;
            }
            curLine ++;
        }
        
        
    }
    
    
}


- (void)createLabelsWithTextArray:(NSArray *)textArray
                          curLine:(NSInteger)curLine
                          canEdit:(BOOL)canEdit
                              tag:(NSInteger)tag {
    
    CGFloat x, y, width, height;
    for (int i = 0; i < textArray.count; i ++) {
        x = kSingleHeight + i * kSingleWidth;
        y = curLine * kSingleHeight;
        width = kSingleWidth;
        height = kSingleHeight;
        UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
        tf.adjustsFontSizeToFitWidth = YES;
        tf.font = FONT(14);
        if (canEdit || i == 0) {
            tf.text = textArray[i];
        } else {
            if ([textArray[i] isEqualToString:STRING(@"shuipingqingjiao")] ||
                [textArray[i] isEqualToString:STRING(@"dingjiao")] ||
                [textArray[i] isEqualToString:STRING(@"jiaoduX")] ||
                [textArray[i] isEqualToString:STRING(@"jiaoduY")]) {
                tf.text = [textArray[i] stringByAppendingString:@"(°)"];
            } else {
                int unit = [ByCommonCache getSystemUnit].intValue;
                NSString *unitstr;
                switch (unit) {
                    case 0:
                        unitstr = @"(m)";
                        if ([textArray[i] isEqualToString:STRING(@"mianji")]) {
                            unitstr = @"(m²)";
                        }
                        if ([textArray[i] isEqualToString:STRING(@"tiji")]) {
                            unitstr = @"(m³)";
                        }
                        break;
                    case 1:
                        unitstr = @"(ft)";
                        if ([textArray[i] isEqualToString:STRING(@"mianji")]) {
                            unitstr = @"(ft²)";
                        }
                        if ([textArray[i] isEqualToString:STRING(@"tiji")]) {
                            unitstr = @"(ft³)";
                        }
                        break;
                    case 2:
                        unitstr = @"(in)";
                        if ([textArray[i] isEqualToString:STRING(@"mianji")]) {
                            unitstr = @"(ft²)";
                        }
                        if ([textArray[i] isEqualToString:STRING(@"tiji")]) {
                            unitstr = @"(ft³)";
                        }
                        break;
                    case 3:
                        unitstr = @"(ft+in)";
                        if ([textArray[i] isEqualToString:STRING(@"mianji")]) {
                            unitstr = @"(ft²)";
                        }
                        if ([textArray[i] isEqualToString:STRING(@"tiji")]) {
                            unitstr = @"(ft³)";
                        }
                        break;
                    default:
                        break;
                }
                tf.text = [textArray[i] stringByAppendingString:unitstr];
            }
        }
        tf.textColor = canEdit ? [UIColor blackColor] : [UIColor orangeColor];
        tf.textAlignment = NSTextAlignmentCenter;
        tf.userInteractionEnabled = canEdit;
        [self addSubview:tf];
        
        tf.tag = tag + i + 1;
        [tf addTarget:self action:@selector(modify:) forControlEvents:UIControlEventEditingDidEnd];
    }
}
- (void)modify:(UITextField *)tf {
    
    NSInteger tag1 = tf.tag / 100;
    NSInteger tag2 = (tf.tag % 100) / 10 - 1;
    NSInteger tag3 = tf.tag % 100 % 10;
    NSMutableArray *temp;
    switch (tag1) {
        case 1:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"L1"]];
            ByBlueToothMeaturesL1Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.length = [self lenthStrToFloat:tf.text];
                    
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
        case 2:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"L2"]];
            ByBlueToothMeaturesL2Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.lengthMin = [self lenthStrToFloat:tf.text];
                    break;
                case 3:
                    model.lengthMax = [self lenthStrToFloat:tf.text];
                    break;
                case 4:
                    model.lengthLast = [self lenthStrToFloat:tf.text];
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
        case 3:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"L4"]];
            ByBlueToothMeaturesL3Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.length = [self lenthStrToFloat:tf.text];
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
        case 4:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"G1"]];
            ByBlueToothMeaturesG1Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.length1 = [self lenthStrToFloat:tf.text];
                    break;
                case 3:
                    model.length2 = [self lenthStrToFloat:tf.text];
                    break;
                case 4:
                    model.length3 = [self lenthStrToFloat:tf.text];
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
        case 5:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"G2"]];
            ByBlueToothMeaturesG2Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.length1 = [self lenthStrToFloat:tf.text];
                    break;
                case 3:
                    model.length2 = [self lenthStrToFloat:tf.text];
                    break;
                case 4:
                    model.length3 = [self lenthStrToFloat:tf.text];
                    break;
                case 5:
                    model.length4 = [self lenthStrToFloat:tf.text];
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
        case 6:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"G3"]];
            ByBlueToothMeaturesG3Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.length1 = [self lenthStrToFloat:tf.text];
                    break;
                case 3:
                    model.length2 = [self lenthStrToFloat:tf.text];
                    break;
                case 4:
                    model.length3 = [self lenthStrToFloat:tf.text];
                    break;
                case 5:
                    model.length4 = [self lenthStrToFloat:tf.text];
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
            
        case 7:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"S1"]];
            ByBlueToothMeaturesS1Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.length1 = [self lenthStrToFloat:tf.text];
                    break;
                case 3:
                    model.length2 = [self lenthStrToFloat:tf.text];
                    break;
                case 4:
                    model.area = [self areaStrToFloat:tf.text];
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
            
        case 8:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"S2"]];
            ByBlueToothMeaturesS2Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.length1 = [self lenthStrToFloat:tf.text];
                    break;
                case 3:
                    model.length2 = [self lenthStrToFloat:tf.text];
                    break;
                case 4:
                    model.length3 = [self lenthStrToFloat:tf.text];
                    break;
                case 5:
                    model.area = [self areaStrToFloat:tf.text];
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
            
        case 9:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"S3"]];
            ByBlueToothMeaturesS3Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.topLength = [self lenthStrToFloat:tf.text];
                    break;
                case 3:
                    model.bottomLength = [self lenthStrToFloat:tf.text];
                    break;
                case 4:
                    model.height = [self lenthStrToFloat:tf.text];
                    break;
                case 5:
                    model.area = [self areaStrToFloat:tf.text];
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
            
        case 10:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"V1"]];
            ByBlueToothMeaturesV1Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.length = [self lenthStrToFloat:tf.text];
                    break;
                case 3:
                    model.width = [self lenthStrToFloat:tf.text];
                    break;
                case 4:
                    model.height = [self lenthStrToFloat:tf.text];
                    break;
                case 5:
                    model.volume = [self vStrToFloat:tf.text];
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
        case 11:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"H1"]];
            ByBlueToothMeaturesH1Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.angle = tf.text.floatValue;
                    break;
                case 3:
                    model.length1 = [self lenthStrToFloat:tf.text];
                    break;
                case 4:
                    model.length2 = [self lenthStrToFloat:tf.text];
                    break;
                case 5:
                    model.length3 = [self lenthStrToFloat:tf.text];
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
        case 12:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"H2"]];
            ByBlueToothMeaturesH2Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.angle = tf.text.floatValue;
                    break;
                case 3:
                    model.hypotenuseLength = [self lenthStrToFloat:tf.text];
                    break;
                case 4:
                    model.horizontalLength = [self lenthStrToFloat:tf.text];
                    break;
                case 5:
                    model.verticalLength = [self lenthStrToFloat:tf.text];
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
        case 13:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"X1"]];
            ByBlueToothMeaturesX1Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.angle1 = tf.text.floatValue;
                    break;
                case 3:
                    model.angle2 = tf.text.floatValue;
                    break;
                case 4:
                    model.length = [self lenthStrToFloat:tf.text];
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
        case 14:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"P1"]];
            ByBlueToothMeaturesP1Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.length1 = [self lenthStrToFloat:tf.text];
                    break;
                case 3:
                    model.length2 = [self lenthStrToFloat:tf.text];
                    break;
                case 4:
                    model.length3 = [self lenthStrToFloat:tf.text];
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
        case 15:
        {
            temp = [NSMutableArray arrayWithArray:self.meatureModelsDic[@"M1"]];
            ByBlueToothMeaturesM1Model *model = temp[tag2];
            switch (tag3) {
                case 1:
                    model.name = tf.text;
                    break;
                case 2:
                    model.length1 = [self lenthStrToFloat:tf.text];
                    break;
                case 3:
                    model.length2 = [self lenthStrToFloat:tf.text];
                    break;
                case 4:
                    model.length3 = [self lenthStrToFloat:tf.text];
                    break;
                default:
                    break;
            }
            [temp replaceObjectAtIndex:tag2 withObject:model];
        }
            break;
            
        default:
            break;
    }
    
}
- (CGFloat)lenthStrToFloat:(NSString *)lengthStr {
    int unit = [ByCommonCache getSystemUnit].intValue;
    switch (unit) {
        case 0: // 米
            return lengthStr.doubleValue;
            break;
        case 1: // 英尺
            
//            return lengthStr.floatValue / 3.2808399;

            return lengthStr.doubleValue * 0.0254 * 12;


            break;
        case 2: // 英寸
//            return lengthStr.floatValue / (3.2808399 * 12);
            return lengthStr.doubleValue * 0.0254;

            break;
        case 3: // 英尺+英寸
        {
            NSString *s1 = [lengthStr componentsSeparatedByString:@"\'"].firstObject;
            NSString *s2 = [lengthStr componentsSeparatedByString:@"\'"].lastObject;
            s2 = [s2 substringToIndex:s2.length - 1];
            CGFloat f = (s1.doubleValue + (s2.doubleValue) / 12.000) / 3.2808399;
            return f;
        }
            break;
        default:
            break;
    }
    return 0;
}
- (CGFloat)areaStrToFloat:(NSString *)areaStr {
    
    int unit = [ByCommonCache getSystemUnit].intValue;
    switch (unit) {
        case 0: // 米
            return areaStr.floatValue;
            break;
        case 1: // 英尺
        case 2: // 英寸
        case 3: // 英尺+英寸
            return areaStr.floatValue / pow(3.2808399, 2);
            break;
        default:
            break;
    }
    return 0;
}
- (CGFloat)vStrToFloat:(NSString *)vStr {
    
    int unit = [ByCommonCache getSystemUnit].intValue;
    switch (unit) {
        case 0: // 米
            return vStr.floatValue;
            break;
        case 1: // 英尺
        case 2: // 英寸
        case 3: // 英尺+英寸
            return vStr.floatValue / pow(3.2808399, 3);
            break;
        default:
            break;
    }
    return 0;
}

- (NSArray *)textArrayFromModel:(ByBlueToothMeaturesModel *)model {
    
    if ([model isKindOfClass:[ByBlueToothMeaturesL1Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesL1Model *)model).lengthStr];
    }
    if ([model isKindOfClass:[ByBlueToothMeaturesL2Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesL2Model *)model).lengthMinStr,
                 ((ByBlueToothMeaturesL2Model *)model).lengthMaxStr,
                 ((ByBlueToothMeaturesL2Model *)model).lengthLastStr];
    }
    if ([model isKindOfClass:[ByBlueToothMeaturesL3Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesL3Model *)model).lengthStr];
    }
    if ([model isKindOfClass:[ByBlueToothMeaturesG1Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesG1Model *)model).length1Str,
                 ((ByBlueToothMeaturesG1Model *)model).length2Str,
                 ((ByBlueToothMeaturesG1Model *)model).length3Str];
    }
    if ([model isKindOfClass:[ByBlueToothMeaturesG2Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesG2Model *)model).length1Str,
                 ((ByBlueToothMeaturesG2Model *)model).length2Str,
                 ((ByBlueToothMeaturesG2Model *)model).length3Str,
                 ((ByBlueToothMeaturesG2Model *)model).length4Str];
    }
    if ([model isKindOfClass:[ByBlueToothMeaturesG3Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesG3Model *)model).length1Str,
                 ((ByBlueToothMeaturesG3Model *)model).length2Str,
                 ((ByBlueToothMeaturesG3Model *)model).length3Str,
                 ((ByBlueToothMeaturesG3Model *)model).length4Str];
    }
    if ([model isKindOfClass:[ByBlueToothMeaturesS1Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesS1Model *)model).length1Str,
                 ((ByBlueToothMeaturesS1Model *)model).length2Str,
                 ((ByBlueToothMeaturesS1Model *)model).areaStr];
    }
    if ([model isKindOfClass:[ByBlueToothMeaturesS2Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesS2Model *)model).length1Str,
                 ((ByBlueToothMeaturesS2Model *)model).length2Str,
                 ((ByBlueToothMeaturesS2Model *)model).length3Str,
                 ((ByBlueToothMeaturesS2Model *)model).areaStr];
    }
    if ([model isKindOfClass:[ByBlueToothMeaturesS3Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesS3Model *)model).topLengthStr,
                 ((ByBlueToothMeaturesS3Model *)model).bottomLengthStr,
                 ((ByBlueToothMeaturesS3Model *)model).heightStr,
                 ((ByBlueToothMeaturesS3Model *)model).areaStr];
    }
   
    
    if ([model isKindOfClass:[ByBlueToothMeaturesV1Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesV1Model *)model).lengthStr,
                 ((ByBlueToothMeaturesV1Model *)model).widthStr,
                 ((ByBlueToothMeaturesV1Model *)model).heightStr,
                 ((ByBlueToothMeaturesV1Model *)model).volumeStr];
    }
    if ([model isKindOfClass:[ByBlueToothMeaturesH1Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesH1Model *)model).angleStr,
                 ((ByBlueToothMeaturesH1Model *)model).length1Str,
                 ((ByBlueToothMeaturesH1Model *)model).length2Str,
                 ((ByBlueToothMeaturesH1Model *)model).length3Str];
    }
    if ([model isKindOfClass:[ByBlueToothMeaturesH2Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesH2Model *)model).angleStr,
                 ((ByBlueToothMeaturesH2Model *)model).hypotenuseLengthStr,
                 ((ByBlueToothMeaturesH2Model *)model).horizontalLengthStr,
                 ((ByBlueToothMeaturesH2Model *)model).verticalLengthStr];
    }
    if ([model isKindOfClass:[ByBlueToothMeaturesX1Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesX1Model *)model).angle1Str,
                 ((ByBlueToothMeaturesX1Model *)model).angle2Str,
                 ((ByBlueToothMeaturesX1Model *)model).lengthStr];
    }
    if ([model isKindOfClass:[ByBlueToothMeaturesP1Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesP1Model *)model).length1Str,
                 ((ByBlueToothMeaturesP1Model *)model).length2Str,
                 ((ByBlueToothMeaturesP1Model *)model).length3Str];
    }
    if ([model isKindOfClass:[ByBlueToothMeaturesM1Model class]]) {
        return @[model.name,
                 ((ByBlueToothMeaturesM1Model *)model).length1Str,
                 ((ByBlueToothMeaturesM1Model *)model).length2Str,
                 ((ByBlueToothMeaturesM1Model *)model).length3Str];
    }
    return @[];
}

@end
